<template>
  <div class="banner">
    <Navbar :title="title" :xiaoming="reserve" />
    <el-form :inline="true" ref="formRes" :model="formInline" class="demo-form-inline">
      <el-form-item label="位置" prop="position">
        <el-input v-model="formInline.position" placeholder="请输入所在位置"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit('formRes')">查询</el-button>
        <el-button @click="resetForm()">重置</el-button>
      </el-form-item>
    </el-form>
    <el-button type="success" @click="dialog = true" class="xz">新增</el-button>
    <el-table :data="tableData" style="width: 100%">
      <el-table-column prop="bid" label="ID">
      </el-table-column>
      <el-table-column label="图片路径">
        <template slot-scope="scope">
          <img :src="scope.row.img" alt="">
        </template>
      </el-table-column>
      <el-table-column prop="imglocal" label="图片位置">
      </el-table-column>
      <el-table-column label="上架日期">
        <template slot-scope="scope">
          <i class="el-icon-time"></i>
          <span style="margin-left: 10px">{{ scope.row.aftertime }}</span>
        </template>
      </el-table-column>
      <el-table-column label="下架日期">
        <template slot-scope="scope">
          <i class="el-icon-time"></i>
          <span style="margin-left: 10px">{{ scope.row.beforetime }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态">
        <template slot-scope="scope">
          <el-tooltip placement="top">
            <el-switch v-model="scope.row.bool" active-color="#0099ff" inactive-color="#cccccc" :active-value="1"
              :inactive-value="0"></el-switch>
          </el-tooltip>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
      :current-page.sync="query.current" :page-sizes="[5, 10, 15, 20]" :page-size="query.size"
      layout="total, sizes, prev, pager, next, jumper" :total="total">
    </el-pagination>
    <el-dialog title="提示" :visible.sync="dialog" width="30%">

      <el-form :model="form" ref="formRef" >
        图片路径
        <el-upload  class="avatar-uploader" action="https://jsonplaceholder.typicode.com/posts/" :show-file-list="false"
          :on-success="handleAvatarSuccess" :before-upload="beforeAvatarUpload">
          <img v-if="form.img" width="100%" :src="form.img" class="post">
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
        <el-form-item label="图片位置" prop="imglocal">
          <el-input v-model="form.imglocal" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="上架时间" required>
          <el-col :span="11">
            <el-form-item prop="aftertime">
              <el-date-picker type="date" placeholder="选择日期" v-model="form.aftertime" style="width: 100%;"
                value-format="yyyy-MM-dd HH:mm:ss"></el-date-picker>
            </el-form-item>
          </el-col>
        </el-form-item>
        <el-form-item label="下架时间" required>
          <el-col :span="11">
            <el-form-item prop="beforetime">
              <el-date-picker type="date" placeholder="选择日期" v-model="form.beforetime" style="width: 100%;"
                value-format="yyyy-MM-dd HH:mm:ss"></el-date-picker>
            </el-form-item>
          </el-col>
        </el-form-item>
        <el-form-item label="上架状态" prop="bool">
          <el-switch v-model="form.bool" active-color="#0099ff" inactive-color="#cccccc" :active-value="1"
            :inactive-value="0"></el-switch>
        </el-form-item>
      </el-form>
      <el-button type="info" @click="cancelForm()">取 消</el-button>
      <el-button type="primary" @click="submitForm('formRef')" :loading="loading">{{
        loading? '提交中 ...': '提 交'
      }}</el-button>
    </el-dialog>
  </div>
</template>

<script>
import Navbar from "../../components/NavBar.vue";
import { gmlist, gmupdate, gmupload, gmdel, gmblursearch } from '@/api/link'
export default {
  components: {
    Navbar,

  },
  data() {
    return {
      total: 0,
      title: "轮播图管理",
      reserve: "返回上一层",
      dialog: false,
      loading: false,
      formInline: {
        position: "",
      },
      tableData: [],
      form: {
        bid: '',
        img: '',
        imglocal: '',
        beforetime: '',
        aftertime: '',
        bool: '',

      },
 
      query: {
        current: 1,
        size: 5,
      },
      list:{}
    }
  },
  created() {
    this.getList();
  },
  methods: {
    //获取列表数据
    getList() {
      gmlist(this.query).then((res) => {
        console.log(res);
        this.total = res.data.total;
        this.tableData = res.data.records
      })
    },
    //查询
    onSubmit() {
      this.list = Object.assign(this.query,this.formInline)
      console.log(this.list);
      gmblursearch(this.list).then((res) => {
        console.log(res);
      })
     },
    //搜索重置
    resetForm() {
      this.$refs.formRes.resetFields()

    },
    //dialog取消按钮
    cancelForm() {
      this.dialog = false;
      this.form = {
        bid: '',
        img: '',
        imglocal: '',
        beforetime: '',
        aftertime: '',
        bool: '',
     }
      this.$refs.formRef.resetFields()
    },
    //dialog提交按钮
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.loading = true;
          if (this.form.uid) {
            // 修改操作
            gmupload(this.form).then(res => {
              this.$message({
                message: '提交成功',
                type: 'success'
              });
              this.loading = false;
              this.dialog = false;
              this.getList();
            }).catch(err => {
              this.loading = false;
            }).catch(err => {
              this.loading = false;
            })
          } else {
            // 新增
            gmupdate(this.form).then(res => {
              // updateRoles(this.form).then(response => {
              this.$message({
                message: '提交成功',
                type: 'success'
              });
              this.loading = false;
              this.dialog = false;
              this.getList()
              // })
            }).catch(err => {
              this.loading = false;
            })
          }
        } else {
          return false;
        }
      });

    },
    //上传图片
    handleAvatarSuccess(res, file) {
        this.form.img = URL.createObjectURL(file.raw);
      },
      beforeAvatarUpload(file) {
        const isJPG = file.type === 'image/jpeg';
        const isLt2M = file.size / 1024 / 1024 < 2;

        if (!isJPG) {
          this.$message.error('上传头像图片只能是 JPG 格式!');
        }
        if (!isLt2M) {
          this.$message.error('上传头像图片大小不能超过 2MB!');
        }
        return isJPG && isLt2M;
      },
    //分页
    handleSizeChange(val) {
      this.query.current = 1;
      this.query.size = val
      console.log(this.query);
      this.getList();
      // console.log(`每页 ${val} 条`);
    },
    handleCurrentChange(val) {
      this.query.current = val;
      this.getList();
      // console.log(`当前页: ${val}`);
    },

    //编辑
    handleEdit(row) {
      this.dialog = true;
      this.form = JSON.parse(JSON.stringify(row));
    },
    //删除
    handleDelete( row) {
      console.log( row);
            this.$confirm("此操作将永久删除, 是否继续？", "提示", {
                confirmButtonText: "确认",
                cancelButtonText: "取消",
                type: "warning",
            }).then(() => {
              console.log(row);
                
                gmdel({id:row.bid}).then((res) => {
                    console.log(res);

                   
                        this.$message({
                            showClose: true,
                            message: res.msg,
                            type: 'success'
                        });
                        this.getList();
                    
                });
            })
    }
  },
}
</script>

<style lang="scss" scoped>
.banner {
  margin: 0 20px;
}

.demo-form-inline {
  margin-top: 20px;
  margin-left: 20px;
  border-bottom: 3px #fff solid;
}

.xz {
  margin: 20px 20px;
}

.el-table .cell {
  width: 100px;
  height: 100px;

  img {
    width: 50%;
  }
}
.avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>